package com.glorious.controller;

import com.glorious.common.Result;
import com.glorious.common.ResultCode;
import com.glorious.dto.response.appuser.AppUserAuthResponse;
import com.glorious.entities.AppUser;
import com.glorious.service.IAppUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/app/users")
public class AppUserController {

    @Autowired
    private IAppUserService iAppUserService;

    @ApiOperation(value = "新增用户")
    @ApiImplicitParam(name = "appUser", value = "实体JSON对象", required = true, paramType = "body", dataType = "AppUser")
    @PostMapping
    public Result<Long> add(@RequestBody AppUser appUser) {
        boolean status = iAppUserService.save(appUser);
        if (status) {
            return Result.success(appUser.getId());
        } else {
            return Result.failed();
        }
    }

    @ApiOperation(value = "根据openId获取用户认证信息")
    @ApiImplicitParam(name = "openId", value = "微信身份唯一标识", required = true, paramType = "path", dataType = "String")
    @GetMapping("/openId/{openId}")
    public Result<AppUserAuthResponse> getByOpenId(@PathVariable String openId) {
        AppUserAuthResponse response = iAppUserService.getByOpenId(openId);
        if (response == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }
        return Result.success(response);
    }

    @ApiOperation(value = "根据手机号获取用户认证信息")
    @ApiImplicitParam(name = "mobile", value = "用户手机号码", required = true, paramType = "path", dataType = "String")
    @GetMapping("/mobile/{mobile}")
    public Result<AppUserAuthResponse> getByMobile(@PathVariable String mobile) {
        AppUserAuthResponse response = iAppUserService.getByMobile(mobile);
        if (response == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }
        return Result.success(response);
    }
}
